package com.tanhua.dubbo.api;

import com.tanhua.commons.utils.Constants;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override//查询评论列表
    public List<Comment> findComments(String movementId, Integer commentType, Integer page, Integer pagesize) {
        //1.构建Cr
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                                    .and("commentType").is(commentType);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        return mongoTemplate.find(query,Comment.class);
    }

    @Override//保存互动数据，返回互动总数
    public Integer save(Comment comment) {
        Integer commentType = comment.getCommentType(); //互动类型
        //a、保存互动数据
        ObjectId movementId = comment.getPublishId();
        //查询动态对象
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        //设置动态发布人id到comment
        comment.setPublishUserId(movement.getUserId());
        //保存
        mongoTemplate.save(comment);

        //b、更新动态，获取最新的数据
        //条件
        Criteria criteria = Criteria.where("id").is(movementId);
        Query query = Query.query(criteria);
        //更新字段
        Update update = new Update();
        if(commentType == 1) {
            update.inc("likeCount",1);
        }else if (commentType ==2) {
            update.inc("commentCount",1);
        }else{
            update.inc("loveCount",1);
        }
        //更新配置
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true); //当更新成功之后，获取最新的数据对象
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);//查询条件，更新的字段内容,配置，实体类
        if(commentType == 1) {
            return modify.getLikeCount();
        }else if (commentType ==2) {
            return modify.getCommentCount();
        }else{
            return modify.getLoveCount();
        }
    }

    @Override//取消点赞，取消喜欢
    public Integer delete(Long userId, String movementId, Integer commentType) {
        //第一步 删除互动数据，构建条件
        Criteria criteria = Criteria
                .where("publishId").is(new ObjectId(movementId))
                .and("userId").is(userId)
                .and("commentType").is(commentType);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        //第二步 更新动态表
        //1.构建Query
        Criteria criteriaLike = Criteria.where("id").is(movementId);
        Query queryLike = Query.query(criteriaLike);
        //2.构建一个update对象
        Update update = new Update();
        //2.1判断一下互动类型
        if (commentType==1){
            update.inc("likeCount",-1);
        }else if (commentType==2){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //3.构建修改后立即返回总数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true); //当更新成功之后，获取最新的数据对象
        Movement modify = mongoTemplate.findAndModify(queryLike, update, options, Movement.class);
        //4.返回参数
        if (commentType==1){
            return modify.getLikeCount();
        }else if (commentType==2){
            return modify.getCommentCount();
        }else {
            return modify.getLoveCount();
        }
    }

    @Override//对评论进行点赞
    public Integer commentLike(String id) {
        Integer count=0;
        //1.判空
        if (!StringUtils.isEmpty(id)){
            //对数据库进行修改
            //1.构建条件
            Criteria criteria = Criteria.where("id").is(new ObjectId(id));
            //2.构建Query
            Query query = Query.query(criteria);
            //3.构建update对象
            Update update = new Update();
            update.inc("likeCount",1);
            //4.对数据库进行修改,修改后立即返回值
            FindAndModifyOptions options = new FindAndModifyOptions();
            options.returnNew(true); //当更新成功之后，获取最新的数据对象
            Comment comment = mongoTemplate.findAndModify(query, update, options, Comment.class);
            //5.从对象中获取到likeCount
            count = comment.getLikeCount();
        }
        return count;
    }

    @Override//取消对评论的点赞
    public Integer disLike(String id) {
        Integer count=0;
        //1.判空
        if (!StringUtils.isEmpty(id)){
            //2.构建条件
            Criteria criteria = Criteria.where("id").is(new ObjectId(id));
            //3.构建Query
            Query query = Query.query(criteria);
            //4.构建Update对象
            Update update = new Update();
            update.inc("likeCount",-1);
            //5.调用findAndModify方法更新数据库，并返回最新结果
            FindAndModifyOptions options = new FindAndModifyOptions();
            options.returnNew(true);
            Comment comment = mongoTemplate.findAndModify(query, update, options, Comment.class);
            //6.从返回的最新对象中获取到点赞数
              count = comment.getLikeCount();
        }
        return count;
    }
}
